import pandas as pd
import numpy as np

def analyze_excel_structure(file_path):
    """分析Excel文件结构，特别是处理合并单元格的标题行"""
    print(f"\n分析文件: {file_path}")
    print("="*50)
    
    try:
        # 读取前几行来分析结构
        df_raw = pd.read_excel(file_path, header=None, nrows=10)
        print("前10行原始数据:")
        print(df_raw)
        print("\n")
        
        # 尝试不同的header设置
        for header_row in [0, 1, 2, [1,2]]:
            try:
                print(f"尝试header={header_row}:")
                df = pd.read_excel(file_path, header=header_row)
                print(f"列名: {list(df.columns)}")
                print(f"数据形状: {df.shape}")
                if len(df) > 0:
                    print(f"第一行数据: {df.iloc[0].to_dict()}")
                print("-"*30)
            except Exception as e:
                print(f"header={header_row} 失败: {e}")
                print("-"*30)
        
        # 尝试跳过前几行
        for skip_rows in [0, 1, 2, 3]:
            try:
                print(f"尝试skiprows={skip_rows}:")
                df = pd.read_excel(file_path, skiprows=skip_rows)
                print(f"列名: {list(df.columns)}")
                print(f"数据形状: {df.shape}")
                if len(df) > 0:
                    print(f"第一行数据: {df.iloc[0].to_dict()}")
                print("-"*30)
            except Exception as e:
                print(f"skiprows={skip_rows} 失败: {e}")
                print("-"*30)
                
    except Exception as e:
        print(f"读取文件失败: {e}")

if __name__ == "__main__":
    # 分析两个Excel文件
    files = [
        "d:/pycode/ding/宸著锦庭6月考勤.xlsx",
        "d:/pycode/工资表.xlsx"
    ]
    
    for file_path in files:
        analyze_excel_structure(file_path)
        print("\n" + "="*80 + "\n")